<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: newsletter_products.php 73 2013-09-21 12:00:48Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 73 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:00:48 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require_once(DIR_FS_INC.'inc.parse_input_field_data.php');

switch($_GET['action']){
	case 'delete':

		if(isset($_GET['acid']) && is_numeric($_GET['acid'])){
			$db->db_query("DELETE FROM ".TABLE_NEWSLETTER_PRODUCT_LIST." WHERE id = '".(int)$_GET['acid']."'");
			$db->db_query("DELETE FROM ".TABLE_NEWSLETTER_PRODUCTS." WHERE accessories_id = '".(int)$_GET['acid']."'");

			redirect(href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS));
		}
		break;

	case 'new':

		if(isset($_POST['products_list'])){								
			$exist = $db->db_query("SELECT id from ".TABLE_NEWSLETTER_PRODUCT_LIST." WHERE list_name = '".$_POST['products_list']."'");

			if($exist->_numOfRows == 0){
				$product_list_array = array('list_name' => $_POST['products_list']);									
				$db->db_perform(TABLE_NEWSLETTER_PRODUCT_LIST, $product_list_array);					
				
				$select_id_query = $db->db_query("SELECT id from ".TABLE_NEWSLETTER_PRODUCT_LIST." WHERE list_name = '".$_POST['products_list']."'");

				redirect(href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=new&aid='.(int)$select_id->fields['id'].'&name='.$_POST['products_list']));
			}else{
				redirect(href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$exist->fields['id']));
			break;
			}							
		}		
		break;

	case 'edit':
		
		
		
		if($_POST['acp']){
			$anz = count($_POST['acp']);
			
			if($anz > 0){
				for($i=0; $i<$anz; $i++){
					$db->db_query("DELETE FROM ".TABLE_NEWSLETTER_PRODUCTS." WHERE id = '".$_POST['acp'][$i]."'");
				}
				
				redirect(href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$_GET['acid']));
			break;
			}
		}

		if(isset($_POST['accessories_product'])){
			$accessories_product = $db->db_query_limit("SELECT 
															p.products_id, 
															p.products_model,
															pd.products_name 
														FROM 
															".TABLE_PRODUCTS." p, 
															".TABLE_PRODUCTS_DESCRIPTION." pd 
														WHERE 
															p.products_status = 1 
														AND 
															p.products_id = pd.products_id 
														AND 
															pd.language_id = '".$_SESSION['languages_id']."' 
														AND 
															pd.products_name LIKE '%".$_POST['accessories_product']."%' 
														OR 
															p.products_model LIKE '%".$_POST['accessories_product']."%' 
														ORDER BY 
															p.products_id ASC", 10);

		}

		if($_POST['accessories']){
			$n = count($_POST['accessories']);
			
			if($n > 0){

				for($i = 0; $i < $n; $i++){
					$accessories_product_array = array(	'accessories_id'	=> (int)$_GET['acid'],
														'product_id' 		=> $_POST['accessories'][$i]);
		
					$db->db_perform(TABLE_NEWSLETTER_PRODUCTS, $accessories_product_array);
				}
				redirect(href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$_GET['acid']));
			break;
			}
		}
		echo '<pre>';
		print_r($_POST);
		echo '</pre>';
		
		echo $n.' - hose';
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
<script type="text/javascript">
<!--
function checkboxes(wert){
	var my = document.leiste;
	var len = my.length;

	for (var i = 0; i < len; i++) {
		var e = my.elements[i];
		if (e.name == "status[]") {
			e.checked = wert;
		}
	}
}
//-->
</script>	
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
	<div id="wrapper">
		<table class="outerTable" cellpadding="0" cellspacing="0">
			<tr>
				<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
					<table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
						<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
					</table>
				</td>
				<td class="boxCenter" valign="top">
					<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
						<tr>
							<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
						</tr>
					</table>
					<table width="100%" border="0" cellspacing="1" cellpadding="2">
						<tr>
							<td class="main"><?php echo CONTENT_NOTE; ?></td>
						</tr>  
					</table><br />
					<table border="0" cellspacing="5" cellpadding="5">
						<tr>
							<td>
								<a class="button" href="<?php echo href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS); ?>"><?php echo LIST_OVERVIEW; ?></a>
							</td>
							<td>
								<a class="button" href="<?php echo href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS,'action=new')?>"><?php echo LIST_NEW; ?></a>
							</td>
						</tr>  
					</table><br />

					<?php
					if($_GET['action'] == 'new'){
						echo draw_form('search',FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=new','post','');
					?>
					
						<table border="0" cellspacing="1" cellpadding="2">
							<tr>
								<td class="access_step_n" width="20"><b>1.</b></td>
								<td class="access_step_c" width="200"><?php echo STEP_1;?></td>

								<?php 
								if($_GET['name']){
									$SELECT = $db->db_query("	SELECT 
																	list_name 
																FROM 
																	".TABLE_NEWSLETTER_PRODUCT_LIST." 
																WHERE 
																	id = '".(int)$_GET['aid']."'");
																	
									echo '<td class="main">'.$select->fields['list_name'];
								}else
									echo '<td class="access_step_c">'.draw_input_field('products_list', '', 'size="25"');
								?>	
				</td>
								
								<?php if(!$_GET['name']){ ?>	
								
								<td class="access_step_c">
									<input class="button" type="submit" onclick="this.blur();" value="<?php echo INPUT_PRODUCT;?>">
								</td>
							
								<?php } ?>		

							</tr> 
							
							<?php if(!$_GET['name']) { ?>

							<tr>
								<td class="main" colspan="4"><?php echo STEP_1_HELP;?></td>  
							</tr>

							<?php } ?>
						</table>
					</form>  

					<?php 
					if($_GET['name']){
						echo draw_form('search',FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$_GET['aid'],'post','');
					?>
							<table border="0" cellspacing="1" cellpadding="2">
								<tr>
									<td class="access_step_n" width="20"><b>2.</b></td>
									<td class="access_step_c" width="200"><?php echo STEP_2a;?></td>
									<td>
										<input class="button" type="submit" onClick="this.blur();" value="<?php echo SEARCH;?>">
									</td>
								</tr>  
							
							<?php if($_GET['name']) { ?>

								<tr>
									<td class="main"  colspan="4"><?php echo STEP_2_HELP;?></td>  
								</tr>

							<?php } ?>
					
							</table>
						</form><br />

					<?php } ?>

					<?php

					}elseif($_GET['action'] == 'edit' && is_numeric($_GET['acid'])){
						echo draw_form('acces',FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$_GET['acid'],'post','');	

						$SELECT = $db->db_query("	SELECT 
														list_name FROM 
													".TABLE_NEWSLETTER_PRODUCT_LIST." 
														WHERE 
													id = '".(int)$_GET['acid']."'");

					?>	

					<table width="100%" border="0" cellspacing="1" cellpadding="0">
						<tr>
							<td valign="top" width="50%">
								<table border="0" cellspacing="1" cellpadding="2" style="border: 1px solid #ccc">
									<tr>
										<td class="dataTableContent_products" width="200" colspan="2"><?php echo $select->fields['list_name'];?></td>	
									</tr>

									<?php
									$select_acc = $db->db_query("	SELECT 
																		ap.id, 
																		p.products_model, 
																		pd.products_name 
																	FROM 
																		".TABLE_PRODUCTS." p, 
																		".TABLE_PRODUCTS_DESCRIPTION." pd,
																		".TABLE_NEWSLETTER_PRODUCT_LIST." a,
																		".TABLE_NEWSLETTER_PRODUCTS." ap  
																	WHERE 
																		a.id = '".(int)$_GET['acid']."' 
																	AND 
																		a.id = ap.accessories_id 
																	AND 
																		ap.product_id = p.products_id 
																	AND 
																		p.products_id = pd.products_id 
																	AND 
																		pd.language_id = '".(int)$_SESSION['languages_id']."'");

									$anz = $select_acc->_numOfRows;		

									while(!$select_acc->EOF){
									?>  
				
									<tr>
										<td class="access_step_cc" width="20">
											<?php echo draw_selection_field('acp[]', 'checkbox', $select_acc->fields['id']);?>
										</td>
										<td class="access_step_cc">
											<?php echo $select_acc->fields['products_model'].' - '.$select_acc->fields['products_name'];?>
										</td>	
									</tr>
	
									<?php 
									$select_acc->MoveNext();
									}
									if($anz > 0){

									?>  
									
									<tr>
										<td colspan="2">
											<button class="button" type="submit" onclick="this.blur();"><?php echo INPUT_DEL_ACCPRODUCT;?></button>
										</td>
									</tr>
									
									<?php } ?>  
								</table>
							</form>

							<td valign="top" width="50%">

							<?php 

								if(isset($_GET['acid']) && is_numeric($_GET['acid'])){
									echo draw_form('search',FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$_GET['acid'],'post','');?>

									<fieldset>
										<legend><h3><?php echo ACCESSORIES_SEARCH;?></h3></legend>
											<table border="0" cellspacing="1" cellpadding="2">
												<tr>
													<td class="access_step_n" width="20"><b>1.</b></td>
													<td class="access_step_c" width="200"><?php echo STEP_2;?></td>
													<td><?php echo draw_input_field('accessories_product', '', 'size="25"', '', '', false);?></td>
													<td><button class="button" type="submit" onClick="this.blur();" ><?php echo SEARCH;?></button></td>
												</tr>  
												</form>
												<?php if(isset($_GET['acid']) && is_numeric($_GET['acid'])){ ?>

												<tr>
													<td class="main" colspan="4"><?php echo STEP_3_HELP;?></td>  
												</tr>
												<?php } ?>
											</table>
										<br />


										<?php 
										if(isset($_POST['accessories_product'])){
											if($accessories_product->_numOfRows > 0){
												echo draw_form('search',FILENAME_MODULE_NEWSLETTER_PRODUCTS, 'action=edit&acid='.(int)$_GET['acid'],'post','');	
										?>
				
										<table border="0" cellspacing="1" cellpadding="2">
											<?php 
											while(!$accessories_product->EOF) {
											?>
											<tr>
												<td width="20">&nbsp;</td>
												<td class="access_step_n" width="20"><?php echo draw_selection_field('accessories[]', 'checkbox', $accessories_product->fields['products_id']);?></td>
												<td class="access_step_c"><?php echo $accessories_product->fields['products_model'];?></td>
												<td class="access_step_c"><?php echo $accessories_product->fields['products_name'];?></td>
											</tr> 
											
											<?php 
											$accessories_product->MoveNext();
											} ?> 
											
											<tr>
												<td colspan="3"><input class="button" type="submit" onclick="this.blur();" value="<?php echo INPUT_PRODUCT;?>"></td>
											</tr>
										</table>
									</fieldset>
								</form>
										<?php } ?>

								<?php
										} 
									} 
								?>

							</td>
						</tr>
					</table>
				<?php	
				}else{ 
				?>
					<table width="100%" class="dataTable" cellspacing="0" cellpadding="0">
						<tr class="dataTableHeadingRow"> 
							<td class="dataTableHeadingContent" width="20"></td>
							<td class="dataTableHeadingContent"><?php echo NAME;?></td>
							<td class="dataTableHeadingContent"><?php echo ACCESSORIES;?></td>
							<td class="dataTableHeadingContent"><?php echo ACTION;?></td>
						</tr> 
				<?php
						$head_product = $db->db_query("SELECT id, list_name FROM ".TABLE_NEWSLETTER_PRODUCT_LIST." ORDER BY id ASC");
						$rows=1;				

						while(!$head_product->EOF){
							$ca = $db->db_query("	SELECT 
														COUNT(ap.id) AS total 
													FROM 
														".TABLE_NEWSLETTER_PRODUCTS." ap, 
														".TABLE_NEWSLETTER_PRODUCT_LIST." a 
													WHERE 
														a.list_name = '".$head_product->fields['list_name']."'
													AND 
														a.id = ap.accessories_id");

							if ($rows % 2 == 0)
								$f = 'dataTableRow';
							else
								$f = '';

							echo '<tr class="'. $f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''. $f.'\'">'."\n";
				?> 
				
							<td class="access_step_nc" width="20"><?php echo draw_selection_field('ids[]', 'checkbox', $head_product->fields['list_name']);?></td>
							<td class="access_step_cc"><nobr><?php echo $head_product->fields['list_name'];?></nobr></td>
							<td class="access_step_cc"><?php echo $ca->fields['total'];?></td>
							<td class="access_step_cc">
								<a href="<?php echo href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS,'action=edit&acid='.(int)$head_product->fields['id'])?>">
									<img src="images/icon_edit.gif" alt="" title="<?php echo ACTION_EDIT;?>" />
								</a>&nbsp;
								<a href="<?php echo href_link(FILENAME_MODULE_NEWSLETTER_PRODUCTS,'action=delete&acid='.(int)$head_product->fields['id'])?>">
									<img src="images/cross.gif" alt="" title="<?php echo ACTION_DEL;?>" />
								</a>
							</td>
						</tr>
						<?php 
							$rows++;
						$head_product->MoveNext();
						}
						?>  
					</table>	
				<?php
				}
				?> 

				</td>
			</tr>
		</table>
		<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
	</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>